package ca.etsmtl.log730.projet2.connectors.msg.document;

import java.util.UUID;

import ca.etsmtl.log730.projet2.connectors.clientserver.RequestManager;
import ca.etsmtl.log730.projet2.connectors.clientserver.nodes.DocumentMap;
import ca.etsmtl.log730.projet2.connectors.clientserver.nodes.DocumentMap.DocumentListener;
import ca.etsmtl.log730.projet2.connectors.msg.Request;
import ca.etsmtl.log730.projet2.connectors.msg.RequestReply;
import ca.etsmtl.log730.projet2.documents.Document;
import ca.etsmtl.log730.projet2.users.User;

public class DocumentCMD extends Request {

	/**
	 * 
	 */
	private static final long serialVersionUID = -18854343542265595L;
	private ModType modType;
	private UUID docId;
	private Document document;
	private User user;
	public DocumentCMD(ModType m, UUID u, Document d, User us) {
		setModType(m);
		setDocId(u);
		setDocument(d);
		setUser(us);
	}
	public ModType getModType() {
		return modType;
	}
	public void setModType(ModType modType) {
		this.modType = modType;
	}
	public UUID getDocId() {
		return docId;
	}
	public void setDocId(UUID docId) {
		this.docId = docId;
	}
	public Document getDocument() {
		return document;
	}
	public void setDocument(Document document) {
		this.document = document;
	}
	public User getUser() {
		return user;
	}
	public void setUser(User user) {
		this.user = user;
	}
	public static enum ModType {
		CHANGE, ADD, REMOVE;
	}
	
	public void apply(DocumentMap dm) {
		if(this.modType == ModType.ADD) {
			dm.put(docId, document);
		} else if (this.modType == ModType.REMOVE) {
			dm.remove(docId);
		} else if(this.modType == ModType.CHANGE) {
			dm.put(docId, document);
		}
		for(DocumentListener dl: dm.getDocumentListeners()) {
			dl.updated(getDocId(), this.getDocument());
		}
	}
	@Override
	public RequestReply createRequestReply(RequestManager rm) {
		return new RequestReply.Accepted(this);
	}

	
}
